package db.tables;


//import java.sql.Statement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import control.ConnectionManage;


import db.parking.beans.Employee;


public class AdminEmployee {
	
	private static Connection conn = ConnectionManage.getInstance().getConnection();

	/*public static void displayAllRows() throws SQLException {

		String sql = "SELECT *  FROM Comlaints";
		try (
				Statement stmt = conn.createStatement();
				ResultSet rs = stmt.executeQuery(sql);
				){

			System.out.println("Complaints Table:");
			while (rs.next()) {
				StringBuffer bf = new StringBuffer();
				bf.append(rs.getInt("id") + ": ");
				bf.append(rs.getString("username") +", ");
				bf.append(rs.getString("age"));
				System.out.println(bf.toString());
			}
		}
	}
*/
	public static Employee getRow(int empId) throws SQLException {

		String sql = "SELECT * FROM Complaints WHERE oid = ?";
		ResultSet rs = null;

		try (
				PreparedStatement stmt = conn.prepareStatement(sql);
				){
			stmt.setInt(1, empId);
			rs = stmt.executeQuery();

			if (rs.next()) {
				Employee bean = new Employee();
				bean.setEmpID(empId);
				bean.setEmpName(rs.getString("empName"));
				bean.setType(rs.getString("type"));
				bean.setLotNo(rs.getInt("lotNo"));
				bean.setStartDate(rs.getString("startDate"));
				bean.setLeaveTime(rs.getString("leaveTime"));
				bean.setStatus(rs.getInt("stauts"));
				
				return bean;
			} else {
				return null;
			}

		} catch (SQLException e) {
			System.err.println(e);
			return null;
		} finally {
			if (rs != null) {
				rs.close();
			}
		}

	}

	//update complaint status
	public static boolean update(Employee bean) throws Exception {

		String sql =
				"UPDATE Employee SET " +
				"status = ? " +
				"WHERE id = ?";
		try (
				PreparedStatement stmt = conn.prepareStatement(sql);
				){
			
			stmt.setInt(1,bean.getStatus());
			stmt.setInt(2,bean.getEmpID());
			
			int affected = stmt.executeUpdate();
			if(affected == 1){
				return true;
			}else{
				return false;
			}
			
		}
		catch(SQLException e) {
			System.err.println(e);
			return false;
		}

	}
	
}
